package org.chen.controller;

import lombok.extern.slf4j.Slf4j;
import org.chen.domain.dto.*;
import org.chen.domain.response.PageResult;
import org.chen.domain.response.Result;
import org.chen.domain.vo.ConfigVO;
import org.chen.domain.vo.MultiAvatarConfigVO;
import org.chen.service.ConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/video/config")
@Slf4j
public class ConfigController {
    
    @Autowired
    private ConfigService configService;

    /**
     * 保存多数字人配置
     */
    @PostMapping("/multi/save")
    public Result<Void> saveMultiAvatarConfig(@RequestBody @Validated MultiAvatarConfigDTO configDTO) {
        try {
            configService.saveMultiAvatarConfig(configDTO);
            return Result.success();
        } catch (Exception e) {
            log.error("Save multi-avatar config failed", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取多数字人配置详情
     */
    @GetMapping("/multi/{id}")
    public Result<MultiAvatarConfigVO> getMultiAvatarConfig(@PathVariable String id) {
        try {
            MultiAvatarConfigVO config = configService.getMultiAvatarConfig(id);
            return Result.success(config);
        } catch (Exception e) {
            log.error("Get multi-avatar config failed", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除多数字人配置
     */
    @DeleteMapping("/multi/{id}")
    public Result<Void> deleteMultiAvatarConfig(@PathVariable String id) {
        try {
            configService.deleteMultiAvatarConfig(id);
            return Result.success();
        } catch (Exception e) {
            log.error("Delete multi-avatar config failed", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取多数字人配置列表
     */
    @GetMapping("/default/multi")
    public Result<List<ConfigVO>> getMultiConfigList() {
        try {
            List<ConfigVO> configs = configService.getMultiConfig();
            return Result.success(configs);
        } catch (Exception e) {
            log.error("Get config list failed", e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 保存配置
     */
    @PostMapping("/save")
    public Result<Void> saveConfig(@RequestBody @Validated ConfigDTO configDTO) {
        try {
            configService.saveConfig(configDTO);
            return Result.success();
        } catch (Exception e) {
            log.error("Save config failed", e);
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取配置列表
     */
    @GetMapping("/list")
    public Result<PageResult<ConfigVO>> getConfigList(ConfigQueryDTO queryDTO) {
        try {
            PageResult<ConfigVO> configs = configService.getConfigList(queryDTO);
            return Result.success(configs);
        } catch (Exception e) {
            log.error("Get config list failed", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 获取默认配置列表
     */
    @GetMapping("/default/list")
    public Result<List<ConfigVO>> getDefaultConfigList() {
        try {
            List<ConfigVO> configs = configService.getDefaultConfig();
            return Result.success(configs);
        } catch (Exception e) {
            log.error("Get config list failed", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 设置默认配置
     */
    @PutMapping("/{id}/default")
    public Result<Void> setDefaultConfig(@PathVariable String id) {
        try {
            configService.setDefaultConfig(id);
            return Result.success();
        } catch (Exception e) {
            log.error("Set default config failed", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 设置字幕
     */
    @PutMapping("/{id}/caption")
    public Result<Void> setCaptionConfig(@PathVariable String id) {
        try {
            configService.setCaptionConfig(id);
            return Result.success();
        } catch (Exception e) {
            log.error("Set default config failed", e);
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 删除配置
     */
    @DeleteMapping("/{id}")
    public Result<Void> deleteConfig(@PathVariable String id) {
        try {
            configService.deleteConfig(id);
            return Result.success();
        } catch (Exception e) {
            log.error("Delete config failed", e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 删除配置
     */
    @DeleteMapping("/batchDelete")
    public Result<Void> batchDelete(@RequestBody BatchConfigDeleteDTO request) {
        try {
            configService.batchDelete(request);
            return Result.success();
        } catch (Exception e) {
            log.error("Delete config failed", e);
            return Result.error(e.getMessage());
        }
    }
}